// 删除对象
const DeleteData = {
    main(selectData,pagnum){
        this.delSingle(selectData);
        this.delAll(pagnum);
    },

    // 单个删除
    delSingle(selectData){
        const J_del = document.getElementsByClassName('J_del');

        for(let i = 0; i < J_del.length; i++){
            J_del[i].onclick = function(){
                let num = this.parentElement.parentElement.children[1].innerHTML;
                for(let key in Data){
                    if(Data[key].userId == num) num = key;
                }
                const J_Data = document.getElementById('J_Data').children;
                Data[num].delete = true;
                J_Data.length === 1 &&  index --;
                Renderer.rendererFn(index, true);
            }
        }
    },

    // 全选和批量删除
    delAll(pagnum){
        // 选择删除
        const J_selectAll = document.getElementById('J_selectAll');
        const J_select = document.getElementsByClassName('J_select');
        const J_delete = document.getElementById('J_delete');

        // 全选
        J_selectAll.onclick = function(){
            if(J_selectAll.dataset.bool === 'true'){
                J_selectAll.style.color = '#000';
                for(let i = 0; i < J_select.length; i++){ 
                    J_select[i].style.color = '#000';
                    J_select[i].dataset.bool = 'false';
                }
                J_selectAll.dataset.bool = 'false';
            }else{
                J_selectAll.style.color = '#FFF';
                for(let i = 0; i < J_select.length; i++){ 
                    J_select[i].style.color = '#FFF';
                    J_select[i].dataset.bool = 'true';
                }
                J_selectAll.dataset.bool = 'true';
            }

            // 判断是否有选中的
            if(selectAllIndex()){
                J_delete.style.display = 'block';
            }else{
                J_delete.style.display = 'none';
            }
        }

        // 单个选择
        for(let i = 0; i < J_select.length; i++){
            J_select[i].onclick = function () { 
                if(this.dataset.bool === 'true'){
                    this.style.color = '#000';
                    this.dataset.bool = 'false';
                }else{
                    this.dataset.bool = 'true';
                    this.style.color = '#fff';
                }

                // 判断是否 没有选中的
                if(selectIndex()){
                    J_selectAll.dataset.bool = 'false';
                    J_selectAll.style.color = '#000';
                }else{
                    J_selectAll.dataset.bool = 'true';
                    J_selectAll.style.color = '#FFF';
                }

                // 判断是否有选中的
                if(selectAllIndex()){
                    J_delete.style.display = 'block';
                }else{
                    J_delete.style.display = 'none';
                }
            }
        }

        // 判断是否有 没有选中的
        function selectIndex(){
            for(let i = 0; i < J_select.length; i++){ 
                if(J_select[i].dataset.bool === 'true'){
                    return false;
                }
            }
            return true;
        }

        // 判断是否有选中的
        function selectAllIndex(){
            for(let i = 0; i < J_select.length; i++){ 
                if(J_select[i].dataset.bool === 'false'){
                    return true;
                }
            }
            return false;
        }

        // 批量删除
        J_delete.onclick = function(){
            let selectArr = [];
            let num = 0;
            
            for(let i = 0; i < J_select.length; i++){ 
                if(J_select[i].dataset.bool === 'false'){
                    selectArr.push(J_select[i].parentElement.parentElement.children[1].innerHTML);
                    num++;
                }
            }
            for(let i = 0; i < selectArr.length; i++){
                for(let j = 0; j < Data.length; j++){
                    if(Data[j].userId == selectArr[i]){
                        Data.splice(j, 1);
                    }
                }
            }

            num == pagnum && index --;

            Renderer.rendererFn(index, true);

            if(!selectIndex() || selectArr.length === 10){
                Renderer.deleteRest();
            }
        }
    },
}